<template>
  <div>
    <!-- Form -->
    <el-button type="text" @click="dialogFormVisible = true; operteType = 'save'">添加轮播图</el-button>

    <el-dialog title="轮播图信息" :visible.sync="dialogFormVisible" @close='handleClose'>
      <el-form :model="carousel_figure">
        <el-form-item label="轮播图ID" :label-width="formLabelWidth">
          <el-input v-model="carousel_figure.id" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="轮播图" :label-width="formLabelWidth">
          <el-input v-model="carousel_figure.url" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="轮播图" :label-width="formLabelWidth">
          <el-upload class="avatar-uploader" action="http://localhost:8080/upload" :show-file-list="false"
            :on-success="handleAvatarSuccess">
            <img v-if="carousel_figure.url" :src="carousel_figure.url" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSave">确 定</el-button>
      </div>
    </el-dialog>

    <el-table :data="carousel_figures" style="width: 100%">
      <el-table-column label="轮播图ID" min-width="100">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column label="轮播图" min-width="100">
        <template slot-scope="scope">
          <img :src="scope.row.url" alt="轮播图" style="width: 100px; height: 100px;">
        </template>
      </el-table-column>


      <el-table-column label="轮播图顺序" min-width="100">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.sequence_num }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="150">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination background layout="prev,pager,next" :total="total" :page-size="pageSize" :current-page="current"
      @current-change="loadCarousel_figure">
    </el-pagination>


    <el-upload class="avatar-uploader" action="http://localhost:8080/upload" :show-file-list="false"
      :on-success="handleAvatarSuccess">
      <img v-if="imageUrl" :src="imageUrl" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>


    </el-upload>


  </div>
</template>
<script>
//引用vue对象
export default {
  name: 'Carousel_figureView',

  //绑定数据
  data() {
    return {
      formLabelWidth: "120px",
      dialogFormVisible: false,
      carousel_figures: [], //数组
      carousel_figure: { id: '', url: '', sequence_num: '' },
      operteType: '',
      total: 1,
      pageSize: 5,
      current: 0,
      imageUrl: ''
    }
  },
  methods: {
    //加载商品
    loadCarousel_figure(current) {
      this.current = current
      this.$http.get('/carousel_figure?m=page&current=' + this.current + "&pageSize=" + this.pageSize).then(resp => {
        console.log(resp.data)
        if (resp.data.data) {
          this.carousel_figures = resp.data.data.records
          this.total = resp.data.data.total
        }
      })
    },
    handleAvatarSuccess(res, file) {
      console.log('upload', res.data)
      this.carousel_figure.url = res.data
    },
    // loadCarousel_figures(){
    //     this.$http.get('/carousel_figure?m=findAll').then(resp => {
    //         console.log(resp.data)
    //         //绑定后端返回的数据
    //         if(resp.data.data){
    //             this.carousel_figures = resp.data.data
    //         }
    //     })
    // },
    handleEdit(index, row) {
      this.operteType = 'updata';
      this.dialogFormVisible = true;
      this.carousel_figure = JSON.parse(JSON.stringify(row));
    },
    handleDelete(index, row) {
      console.log(index, row);
      this.$confirm('是否删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$http.get('/carousel_figure?m=delete&id=' + row.id).then(resp => {
            //绑定后端返回的数据
            if (resp.data.data == true) {
              this.loadCarousel_figure(this.current)
              this.$message({
                type: 'success',
                message: '删除成功'
              });
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    handleSave() {
      if (this.operteType == 'save') {
        this.$http.post('/carousel_figure?m=save', this.$qs.stringify(this.carousel_figure))
          .then(resp => {
            console.log('save carousel_figure', resp.data)
            if (resp.data.data == true) {
              console.log('save successfully')
              this.loadCarousel_figure(this.current);
            }
          })
      } else {
        this.$http.post('/carousel_figure?m=update', this.$qs.stringify(this.carousel_figure))
          .then(resp => {
            console.log('updata carousel_figure', resp.data)
            if (resp.data.data == true) {
              console.log('updata successfully')
              this.loadCarousel_figure(this.current);
            }
          })
      }
      this.dialogFormVisible = false
    },
    handleClose() {
      this.carousel_figure = { id: '', url: '', sequence_num: '' };
      this.operteType = ''
    }
  },
  mounted() {
    this.loadCarousel_figure(1)
  }
}
</script>